Skip to content

don't crash the heartbeat thread when the kernel closes while it's starting - #1611

Open
daphnezlin wants to merge 1 commit into
ipython:mainfrom
daphnezlin:fix-heartbeat-shutdown-race
Open

daphnezlin wants to merge 1 commit into
ipython:mainfrom
daphnezlin:fix-heartbeat-shutdown-race

Conversation

@daphnezlin

@daphnezlin daphnezlin commented Dec 2, 2026 •

Copy link
Copy Markdown

If the kernel is closed right after it starts the heartbeat, the heartbeat thread can still be setting up its socket when the context is terminated. Heartbeat.run() only handles a terminated context inside its main loop, so the thread dies with an uncaught exception from _try_bind_socket:

zmq.error.ContextTerminated: Context was terminated (addr='tcp://127.0.0.1:57089')

or ZMQError: Operation not supported if the context finished terminating before the socket was created.

In the test suite this shows up as rare PytestUnhandledThreadExceptionWarning: Exception in thread Heartbeat errors on teardown in tests/test_curve.py, since those tests call init_heartbeat() and close the app right after. I found it with a flaky test tool I'm building, which reruns the suite in Docker under CPU limits. test_connection_file_no_curve_keys_by_default and test_curve_keys_are_stable_per_startup each failed 2 of 60 runs this way.

Changes:

  • Heartbeat.run() returns quietly if the context is terminated while it's creating or binding the socket, the same as it already does when that happens later in the loop.
  • Two regression tests in tests/test_heartbeat.py: the context terminated before the thread starts, and while it's binding. Both fail on main and pass with this change.

@daphnezlin daphnezlin changed the title don't crash the heartbeat thread when the kernel closes while it's st… don't crash the heartbeat thread when the kernel closes while it's starting Dec 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant